﻿using System;
using System.Windows.Forms;

namespace Encloser.Configuration
{
    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public sealed class ConfigField : Attribute
    {
        private readonly string title;
        private readonly string description;
        private readonly ConfigType controlType;
        private readonly Type enumType;
        private readonly string trueText;
        private readonly string falseText;
        private readonly bool anyColor;
        private readonly bool solidColor;
        private readonly int min1;
        private readonly int max1;
        private readonly double min2;
        private readonly double max2;
        private readonly DateTimePickerFormat timeFormat;
        private readonly string customFormat;

        public string Title { get => title; }
        public string Description { get => description; }
        internal ConfigType ControlType { get => controlType; }
        public Type EnumType { get => enumType; }
        public string TrueText { get => trueText; }
        public string FalseText { get => falseText; }
        public bool AnyColor { get => anyColor; }
        public bool SolidColor { get => solidColor; }
        public int MinInt { get => min1; }
        public int MaxInt { get => max1; }
        public double MinDouble { get => min2; }
        public double MaxDouble { get => max2; }
        public DateTimePickerFormat TimeFormat { get => timeFormat; }
        public string CustomFormat { get => customFormat; }

        public ConfigField(string title, string description = null)
        {
            this.title = title ?? string.Empty;
            this.description = description;
            controlType = ConfigType.Text;
        }

        public ConfigField(string title, bool filePath, string description = null)
        {
            this.title = title ?? string.Empty;
            this.description = description;
            controlType = filePath ? ConfigType.PathFile : ConfigType.PathFolder;
        }

        public ConfigField(string title, int min, int max = int.MaxValue, bool useSlider = false, string description = null)
        {
            this.title = title ?? string.Empty;
            this.description = description;
            min1 = min;
            max1 = max;
            controlType = useSlider ? ConfigType.NumberSimple : ConfigType.Number;
        }

        public ConfigField(string title, double min, double max = double.MaxValue, string description = null)
        {
            this.title = title ?? string.Empty;
            this.description = description;
            min2 = min;
            max2 = max;
            controlType = ConfigType.FloatNumber;
        }

        public ConfigField(string title, string trueText, string falseText, string description = null)
        {
            this.title = title ?? string.Empty;
            this.trueText = trueText ?? "开启";
            this.falseText = falseText ?? "关闭";
            this.description = description;
            controlType = ConfigType.Boolean;
        }

        public ConfigField(string title, bool anyColor, bool solidColor, string description = null)
        {
            this.title = title ?? string.Empty;
            this.anyColor = anyColor;
            this.solidColor = solidColor;
            this.description = description;
            controlType = ConfigType.Color;
        }

        public ConfigField(string title, Type enumType, bool multiple, string description = null)
        {
            this.title = title ?? string.Empty;
            this.enumType = enumType;
            this.description = description;
            controlType = multiple ? ConfigType.EnumMultiple : ConfigType.EnumSingle;
        }

        public ConfigField(string title, DateTimePickerFormat timeFormat, string customFormat = null, string description = null)
        {
            this.title = title ?? string.Empty;
            if (timeFormat == DateTimePickerFormat.Custom && string.IsNullOrEmpty(customFormat)) this.timeFormat = DateTimePickerFormat.Short;
            else this.timeFormat = timeFormat;
            this.customFormat = customFormat;
            this.description = description;
            controlType = ConfigType.Time;
        }
    }
}
